package com.totoro.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.totoro.domain.Student;
import com.totoro.util.Page;

/**
 * 
 * @author zhangpeng
 * 
 * Student#DAO层：学生信息的CRUD方法；
 * 
 */
@Repository
public class StudentDao extends BaseDao<Student>{
	/**
	 * 查询所有学生：升序排列；
	 */
	public List<Student> findAllStudent(){
		String hql = "from Student student order by student.id desc";
		return (List<Student>)(this.findAll(hql));
	}
	
	
    /**
     * 根据用户名查询Student对象；
     */
	@SuppressWarnings("unchecked")
	public List<Student> getStudentByName(String name){
//		String sql = "from Student s where s.name = "+"'"+name+"'";
//		return (List<Student>)(getSession().createQuery(sql).list());
		String sql = "from Student s where s.name = ?";
	    return (List<Student>)(getSession().createQuery(sql).setParameter(0,name).list());
    }
	
	/**
	 * 分页测试：获取分页后查询的学生信息
	 * @param name 查询条件，根据学生查询；
	 * @param pageNo 页号，从1开始；
	 * @param pageSize 每页的记录数；
	 * @return 包含分页信息的Page对象；
	 */
	public Page getPagedPosts(String name,int pageNo,int pageSize) {
		String hql = "from Student s where s.name = ?";
		return pagedQuery(hql,pageNo,pageSize,name);
	}
}
